///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _CDBQUERYRUN_H_
#define _CDBQUERYRUN_H_

namespace buola {

class CDBQueryRun
{
public:
    CDBQueryRun(CDBQuery &pQuery)
        :   mQuery(pQuery)
    {
    }

    CDBQueryRun(CDBQuery *pQuery)
        :   mQuery(*pQuery)
    {
    }

    virtual ~CDBQueryRun()
    {
        mQuery.Reset();
    }

    void Exec()
    {
        return mQuery.Exec();
    }

    bool NextRow()
    {
        return mQuery.NextRow();
    }

    void Bind(int pN,int pValue)
    {
        mQuery.BindInteger(pN,pValue);
    }

    void Bind(int pN,int64_t pValue)
    {
        mQuery.BindInteger(pN,pValue);
    }

    void Bind(int pN,uint32_t pValue)
    {
        mQuery.BindInteger(pN,pValue);
    }

    void Bind(int pN,const std::string &pValue)
    {
        mQuery.BindString(pN,pValue);
    }

    void Bind(int pN,const std::wstring &pValue)
    {
        mQuery.BindString(pN,utf8(pValue));
    }

    template<typename t1>
    void BindAll(const t1 &p1)
    {
        Bind(1,p1);
    }

    template<typename t1,typename t2>
    void BindAll(const t1 &p1,const t2 &p2)
    {
        Bind(1,p1);
        Bind(2,p2);
    }

    template<typename t1,typename t2,typename t3>
    void BindAll(const t1 &p1,const t2 &p2,const t3 &p3)
    {
        Bind(1,p1);
        Bind(2,p2);
        Bind(3,p3);
    }

    template<typename t1,typename t2,typename t3,typename t4>
    void BindAll(const t1 &p1,const t2 &p2,const t3 &p3,const t4 &p4)
    {
        Bind(1,p1);
        Bind(2,p2);
        Bind(3,p3);
        Bind(4,p4);
    }

    template<typename t1,typename t2,typename t3,typename t4,typename t5>
    void BindAll(const t1 &p1,const t2 &p2,const t3 &p3,const t4 &p4,const t5 &p5)
    {
        Bind(1,p1);
        Bind(2,p2);
        Bind(3,p3);
        Bind(4,p4);
        Bind(5,p5);
    }

    void ResetBindings()
    {
        mQuery.ResetBindings();
    }

    template<typename tType>
    tType Column(int pN);

    int64_t GetLastInsertID()
    {
        return mQuery.GetLastInsertID();
    }

private:
    CDBQuery &mQuery;
};

template<>
inline int CDBQueryRun::Column<int>(int pN)
{
    return mQuery.ColumnInteger(pN);
}

template<>
inline int64_t CDBQueryRun::Column<int64_t>(int pN)
{
    return mQuery.ColumnInteger(pN);
}

template<>
inline uint32_t CDBQueryRun::Column<uint32_t>(int pN)
{
    return mQuery.ColumnInteger(pN);
}

template<>
inline std::string CDBQueryRun::Column<std::string>(int pN)
{
    return mQuery.ColumnString(pN);
}

template<>
inline std::wstring CDBQueryRun::Column<std::wstring>(int pN)
{
    return utf32(mQuery.ColumnString(pN));
}

/*namespace buola*/ }

#endif
